<?php

namespace app\admin\model\user;

use app\admin\model\partner\level\Setting;
use Carbon\Carbon;
use think\Model;


class Partner extends Model
{

    

    

    // 表名
    protected $name = 'user_partner';
    
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'integer';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';
    protected $deleteTime = false;

    // 追加属性
    protected $append = [
        'status_text',
        'verify_status_text',
        'is_file_signed_text',
        'jointime',
    ];
    

    
    public function getStatusList()
    {
        return ['0' => __('Status 0'), '1' => __('Status 1'), '2' => __('Status 2')];
    }

    public function getVerifyStatusList()
    {
        return ['0' => __('Verify_status 0'), '1' => __('Verify_status 1'), '2' => __('Verify_status 2'), '3' => __('Verify_status 3')];
    }

    public function getIsFileSignedList()
    {
        return ['0' => __('Is_file_signed 0'), '1' => __('Is_file_signed 1')];
    }

    public function getJointimeAttr($value, $data)
    {
        return Carbon::createFromTimestamp($data['createtime'])->toDateString();
    }

    public function getStatusTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['status']) ? $data['status'] : '');
        $list = $this->getStatusList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getVerifyStatusTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['verify_status']) ? $data['verify_status'] : '');
        $list = $this->getVerifyStatusList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getIsFileSignedTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['is_file_signed']) ? $data['is_file_signed'] : '');
        $list = $this->getIsFileSignedList();
        return isset($list[$value]) ? $list[$value] : '';
    }

    public static function getTheParentPath($partnerId)
    {
        $path = $partnerId;
        for ($i = 0; $i < 10; $i++) {
            $upper = Partner::get($partnerId);
            if ($upper and !is_null($upper->parent_id)) {
                $partnerId = $upper->parent_id;
                $path .= ',' . $upper->parent_id;
            } else {
                break;
            }
        }
        return $path;
    }

    public static function getDreParentPath($dreId)
    {
        $path = $dreId;
        for ($i = 0; $i < 20; $i++) {
            $upper = Partner::get($dreId);
            if ($upper and !is_null($upper->dre_id)) {
                $dreId = $upper->dre_id;
                $path .= ',' . $upper->dre_id;
            } else {
                break;
            }
        }
        return $path;
    }

    public static function getUpLevel($currentLevelId)
    {
        $level = Setting::get($currentLevelId);
        if ($level->level == 3) { // 最高一级，直接返回 同等级ID, 目前最高级为3
            return $currentLevelId; //直接返回最后一级, 同等级ID
        } else {
            $lowerLevel = Setting::get(['level' => $level->level - 1]);
            if ($lowerLevel) {
                return $lowerLevel->id;
            }
        }
    }

    public static function getMaxLevelId()
    {
        // 查找最大等级对应的人
        $maxLevel = 3; 
        $level = Setting::where('level', $maxLevel)->find();
        return $level->id;
    }

    public static function getNextLevel($currentLevelId)
    {
        $level = Setting::get($currentLevelId);
        if ($level->level <= 4) {
            $lowerLevel = Setting::get(['level' => $level->level + 1]);
            if ($lowerLevel) {
                return $lowerLevel->id;
            }
        } else {
            return $currentLevelId; //直接返回最后一级, 同等级ID
        }
    }

    public function user()
    {
        return $this->belongsTo('\\app\\admin\\model\\User', 'user_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }

    public function level()
    {
        return $this->belongsTo('\\app\\admin\\model\\partner\\level\\Setting', 'level_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }

    public function dre()
    {
        return $this->belongsTo('\\app\\admin\\model\\user\\Partner', 'dre_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }

    public function parent()
    {
        return $this->belongsTo('\\app\\admin\\model\\user\\Partner', 'parent_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }


}
